<?php


namespace App\Business;


use Illuminate\Support\Facades\Http;

class LoginController
{

    /**
     * @param $code
     * @return false|string
     */
    static public function theLogin($code)
    {
        $appid = "wxfeb9f6eed848649a";
        $secret = "635a37e769dac3396eed6aa3c366e6ff";

//        根据链接获取openid
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$code."&grant_type=authorization_code";
        $response = Http::get($url);
        $res = $response->json();

//        将获取到的数据进行打包操作
        $data = [
            'code'=>$code,
            'openid'=>$res['openid'],
            'session_key'=>$res['session_key']
        ];
        $lite = Stan::login()->where('openid',$data['openid'])->first();

//        进行判断是否有这条数据
        if($lite)
        {
            Stan::login()->where('openid',$data['openid'])->update(['session_key'=>$data['session_key']]);
        }else{
            Stan::login()->insert($data);
        }

//        生成token
        $token = $lite->createToken('api')->accessToken;

//        将数据进行返回
        return success('成功',$token);
    }
}
